/**
 * @Company 
 * @Title: LoginController.java 
 * @Package com.jbinfo.qingcaowan.app.controller 
 * @author lizhenwei
 * @date 2016-07-11 下午1:06:21 
 * @version V1.0   
 */
package com.jbinfo.qingcaowan.app.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jbinfo.qingcaowan.base.dto.BaseTimeDTO;
import com.jbinfo.qingcaowan.base.dto.UserFullInfoDTO;
import com.jbinfo.qingcaowan.base.service.UserFullService;
import com.jbinfo.qingcaowan.common.ClonePojoUtil;
import com.jbinfo.qingcaowan.common.JSONResult;
import com.jbinfo.qingcaowan.common.LoginContext;
import com.jbinfo.qingcaowan.common.LoginContextUtil;
import com.jbinfo.qingcaowan.common.ResultCode;
import com.jbinfo.qingcaowan.exam.dto.ExamScoreDTO;
import com.jbinfo.qingcaowan.user.dto.ScoreDetailDTO;
import com.jbinfo.qingcaowan.user.dto.UserDTO;
import com.jbinfo.qingcaowan.user.dto.UserManualDTO;
import com.jbinfo.qingcaowan.user.service.ManualService;
import com.jbinfo.qingcaowan.user.service.UserService;

/**
 * @ClassName: LoginController
 * @Description: 用户controller
 * @author lizhenwei
 */
/**
 * @author Administrator
 *
 */
@Controller
public class UserController {

	private static final Logger LOG = LoggerFactory.getLogger(UserController.class);

	@Resource
	private UserService userService;
	@Resource
	private UserFullService userFullService;
	
	@Resource
	private ManualService manualService;
	
	// 用户详细信息
	@RequestMapping(value = "/{client}/findUserInfo")
	public @ResponseBody
	Object findUserInfo(@PathVariable("client") String client, HttpSession session, UserDTO userDTO) {
		LOG.info("客户端为 " + client + " noteDTO：" + userDTO.toString());
		
		LoginContext loginContext = LoginContextUtil.getLoginContext(session);
		
		UserDTO user = loginContext.getLoginUser();
		String userId = user.getUserId();
		userDTO.setUserId(userId);
		
		return userService.getUserInfoById(userDTO);
	}
	
	@RequestMapping(value = "/{client}/a/getUserFullInfo")
	public @ResponseBody
	Object userLogin(@PathVariable("client") String client
			, UserFullInfoDTO userFullInfoDTO
			, HttpServletRequest request, HttpServletResponse response) {
		
		LOG.info("客户端为 " + client + " userFullInfoDTO：" + userFullInfoDTO);
		
		JSONResult dubboResult = (JSONResult) userFullService.getUserFullInfo(userFullInfoDTO);
		
		UserDTO user = new UserDTO();
		
		if (dubboResult != null && ResultCode.SUCCESS.equals(dubboResult.getResultCode())) {
			// 加载用户的登陆信息到session
			UserFullInfoDTO userFullDTO = dubboResult.getResultBean();
			user = ClonePojoUtil.copyClassFromTo(userFullDTO, UserDTO.class);
			
			String username = userFullInfoDTO.getUserName();
			String password = userFullInfoDTO.getPassword();
			// writeUserToSession(user, password,session);
			LoginContextUtil.writeToSession(request, user, username, password);
			
		} else {
			
		}
		// 为解决AngularJs 跨域问题添加
//		response.setHeader("Access-Control-Allow-Origin", "*");// 授权的源控制
//		response.setHeader("Access-Control-Allow-Credentials", "true"); // 控制是否开启与Ajax的Cookie提交方式
//		response.setHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS,DELETE"); // 允许请求的HTTP Method
//		response.setHeader("Access-Control-Max-Age", "3600"); // 授权的时间
//		response.setHeader("Access-Control-Allow-Headers", "x-requested-with,content-type,Accepte"); // 控制哪些header能发送真正的请求  ,content-type   
		
		response.setHeader("Access-Control-Allow-Origin"
				,"*"); // http://my.dj.com   http://www.dotnetdev.net
		
		response.setHeader("Access-Control-Allow-Credentials", "true");
		response.setHeader("Access-Control-Allow-Methods"
				,"PUT, GET, POST, DELETE, OPTIONS");
		response.setHeader("Access-Control-Allow-Headers"
				,"X-Requested-With,Content-Type,Authorization,Accept,Range,Origin");
		response.setHeader("Access-Control-Expose-Headers"
				,"Content-Range");
		response.setHeader("Access-Control-Max-Age","3600");
		
		return dubboResult;
	}
	
	// 更新用户信息
	@RequestMapping(value = "/{client}/updateUser")
	public @ResponseBody
	Object updateUser(@PathVariable("client") String client,
			HttpServletResponse response, UserDTO userDTO) {
		LOG.info("客户端为 " + client + " userDTO：" + userDTO.toString());
		
		return userService.updateUserInfoById(userDTO);
	}
	
	// "我的排名"中前几名及登陆用户的排名
	@RequestMapping(value = "/{client}/findTopAndMyRank")
	public @ResponseBody
	Object findTopAndMyRank(@PathVariable("client") String client,
			HttpServletResponse response, BaseTimeDTO baseTimeDTO) {
		
		LOG.info("客户端为 " + client + " baseTimeDTO：" + baseTimeDTO.toString());
		
		return userService.findTopAndMyRank(baseTimeDTO);
	}
	
	// "积分排名"中前几名及登陆用户的排名   -- 积分
	@RequestMapping(value = "/{client}/findTopAndMyRankByScore")
	public @ResponseBody
	Object findTopAndMyRankByScore(@PathVariable("client") String client,
			HttpServletResponse response, ScoreDetailDTO scoreDetailDTO) {
		
		LOG.info("客户端为 " + client + " scoreDetailDTO：" + scoreDetailDTO.toString());
		
		return userService.findTopAndMyRankByScore(scoreDetailDTO);
	}
	
	// "成绩排名"中前几名及登陆用户的排名  
	@RequestMapping(value = "/{client}/findTopAndMyRankByExam")
	public @ResponseBody
	Object findTopAndMyRankByExam(@PathVariable("client") String client,
			HttpServletResponse response, ExamScoreDTO examScoreDTO) {
		
		LOG.info("客户端为 " + client + " examScoreDTO：" + examScoreDTO.toString());
		
		return userService.findTopAndMyRankByExam(examScoreDTO);
	}
	
	// 支部排名   -- 阅读时间
	@RequestMapping(value = "/{client}/findTopAndMyRankByOrgRead")
	public @ResponseBody
	Object Org(@PathVariable("client") String client,
			HttpServletResponse response, BaseTimeDTO baseTimeDTO) {
		
		LOG.info("客户端为 " + client + " baseTimeDTO：" + baseTimeDTO.toString());
		
		return userService.findTopAndMyRankByOrgRead(baseTimeDTO);
	}
	
	// 支部排名   -- 积分
	@RequestMapping(value = "/{client}/findTopAndMyRankByOrgScore")
	public @ResponseBody
	Object findTopAndMyRankByOrgScore(@PathVariable("client") String client,
			HttpServletResponse response, ScoreDetailDTO scoreDetailDTO) {
		
		LOG.info("客户端为 " + client + " scoreDetailDTO：" + scoreDetailDTO.toString());
		
		return userService.findTopAndMyRankByOrgScore(scoreDetailDTO);
	}
	
	// 支部排名  --  成绩  
	@RequestMapping(value = "/{client}/findTopAndMyRankByOrgExam")
	public @ResponseBody
	Object findTopAndMyRankByOrgExam(@PathVariable("client") String client,
			HttpServletResponse response, ExamScoreDTO examScoreDTO) {
		
		LOG.info("客户端为 " + client + " examScoreDTO：" + examScoreDTO.toString());
		
		return userService.findTopAndMyRankByOrgExam(examScoreDTO);
	}
	
	// 用户在支部排名   -- 阅读时间
	@RequestMapping(value = "/{client}/findTopAndMyRankByReadInSubOrg")
	public @ResponseBody
	Object findTopAndMyRankByReadInSubOrg(@PathVariable("client") String client,
			HttpServletResponse response, BaseTimeDTO baseTimeDTO) {
		
		LOG.info("客户端为 " + client + " baseTimeDTO：" + baseTimeDTO.toString());
		
		return userService.findTopAndMyRankInSubOrg(baseTimeDTO);
	}
		
	// 支部排名   -- 用户在支部中积分
	@RequestMapping(value = "/{client}/findTopAndMyRankByScoreInSubOrg")
	public @ResponseBody
	Object findTopAndMyRankByScoreInSubOrg(@PathVariable("client") String client,
			HttpServletResponse response, ScoreDetailDTO scoreDetailDTO) {
		
		LOG.info("客户端为 " + client + " scoreDetailDTO：" + scoreDetailDTO.toString());
		
		return userService.findTopAndMyRankByScoreInSubOrg(scoreDetailDTO);
	}
	
	// 用户在支部中排名  --  成绩  
	@RequestMapping(value = "/{client}/findTopAndMyRankByExamInSubOrg")
	public @ResponseBody
	Object findTopAndMyRankByExamInSubOrg(@PathVariable("client") String client,
			HttpServletResponse response, ExamScoreDTO examScoreDTO) {
		
		LOG.info("客户端为 " + client + " examScoreDTO：" + examScoreDTO.toString());
		
		return userService.findTopAndMyRankByExamInSubOrg(examScoreDTO);
	}
		
	
	// 查询userId 进行分页查询学分
	@RequestMapping(value = "/{client}/findScoreList")
	public @ResponseBody
	Object findScoreList(@PathVariable("client") String client, 
			HttpServletResponse response,
			ScoreDetailDTO scoreDetailDTO,
			Integer currentPage, Integer pageSize) {
		LOG.info("客户端为 " + client + " scoreDetailDTO：" + scoreDetailDTO.toString());
		
		return userService.findScoreDetailList(scoreDetailDTO, currentPage, pageSize);
	}
	
	// 学员手册内容
	@RequestMapping(value = "/{client}/findManualCont")
	public @ResponseBody
	Object findManualCont(@PathVariable("client") String client,
			HttpServletResponse response, UserDTO userDTO,
			UserManualDTO userManualDTO) {
		
		LOG.info("客户端为 " + client + " userManualDTO：" + userManualDTO.toString());
		
		// 根据userId 查询orgId
		JSONResult userJson = (JSONResult) userService.getUserInfoById(userDTO);
		UserDTO user = userJson.getResultBean();
		String orgId = user.getUserOrg();
		
		userManualDTO.setOrgId(orgId);
		return manualService.findManualContent(userManualDTO);
	}

}
